package com.eco.netty;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @Author: xulinyu
 * @CreateTime: 2024-08-14
 */
@Data
@Component
public class NettyClientConfig {

    @Value("${sensor.tcpserver.ip:}")
    private String sensorHost1;

    @Value("${sensor.tcpserver.port:}")
    private int sensorPort1;

    @Value("${sensor.tcpserver2.ip:}")
    private String sensorHost2;

    @Value("${sensor.tcpserver2.port:}")
    private int sensorPort2;

    private GenericNettyClient client1;
    private GenericNettyClient client2;

    @PostConstruct
    public void init() {
        client1 = new GenericNettyClient("sensor1",sensorHost1, sensorPort1);
        client1.connect();
        client2 = new GenericNettyClient("sensor2",sensorHost2, sensorPort2);
        client2.connect();
    }

    @PreDestroy
    public void destroy() {
        client1.shutdown();
        client2.shutdown();
    }

}
